<template>
  <div class="details-relation-item">
    <el-card class="box-card" shadow="hover">
      <div slot="header">
        <span class="title">相关阅读</span>
      </div>
      <el-carousel indicator-position="none" height="150px" v-loading="loading">
        <template v-if="list.length">
          <el-carousel-item v-for="row in list" :key="row.index">
            <el-row :gutter="15">
              <el-col
                :span="6"
                class="relation-blog-item"
                v-for="item in row"
                :key="item.id"
                @click.native="goBlogdetails(item)"
              >
                <!-- 相关文章没有图片不会显示，具体后续要改再改吧 -->
                <template v-if="item.img">
                  <div class="relation-item-img">
                    <img :src="item.img" />
                  </div>
                  <div class="relation-item-section">
                    <p class="section-title">
                      {{ item.title }}
                    </p>
                  </div>
                </template>
              </el-col>
            </el-row>
          </el-carousel-item>
        </template>
        <template v-else>
          <el-empty v-show="!loading" :image-size="100"></el-empty>
        </template>
      </el-carousel>
    </el-card>
  </div>
</template>

<script>
export default {
  created() {
    // 直接点击热门文章，不会再次请求，所以还需要监听路由变化请求数据
    this.getData()
  },
  beforeRouteUpdate(to, from, next) {
    this.getData()
    next()
  },
  data() {
    return {
      loading: false,
      list: []
    }
  },
  methods: {
    getData() {
      this.loading = true

      this.$ajax({
        url: this.$ajax.setUrl(`/article/${this.$route.params.id}/relation`),
        method: 'get',
        params: this.$ajax.setParams()
      })
        .then(({ data }) => {
          if (data && data.code === 0) {
            this.list = this.filterArr(data.data, 4)
            this.loading = false
          } else {
            this.$message.error(data.data || data.msg)
          }
        })
        .catch(() => {
          this.loading = false
        })
    },
    // 变成4*xxx的数组二维数组
    // num 是每行多少个
    filterArr(arr, num) {
      const col = Math.ceil(arr.length / num)
      const newList = []
      for (var i = 0; i < col; i++) {
        const data = arr.slice(i * num, i * num + num)
        newList.push(data)
      }
      return newList
    },
    goBlogdetails(item) {
      this.$router.push({ name: 'blog-details', params: { id: item.id } })
    }
  }
}
</script>

<style lang="scss">
.details-relation-item {
  .box-card {
    .title {
      color: #888;
      font-size: 16px;
    }
  }
  .relation-blog-item {
    &:hover {
      .relation-item-img {
        img {
          transform: scale(1.03);
        }
      }
      .section-title {
        color: $theme-blue;
      }
      .section-content {
        color: #333 !important;
      }
    }
    .relation-item-img {
      width: 100%;
      height: 100px;
      border-radius: 5px;
      overflow: hidden;
      cursor: pointer;
      img {
        transition: all 0.3s ease-out !important;
        height: 100%;
        width: 100%;
        object-fit: cover;
      }
    }
    .relation-item-section {
      padding: 15px 20px 0 20px;
      color: #000;
      text-align: center;
      font-weight: normal;
      .section-title {
        font-size: 15px;
        cursor: pointer;
        white-space: nowrap;
        overflow: hidden;
        text-overflow: ellipsis;
      }
    }
  }

  .el-empty {
    padding: 0;
    .el-empty__description {
      font-size: 12px;
      color: #f4f4f4;
    }
  }
}
</style>
